/*
 * @Description:
 * @Date: 2022-11-07 14:42:41
 * @LastEditors: czp
 * @LastEditTime: 2023-03-24 16:05:33
 */

import { RouteRecord } from "@/router/interface";

/**
 * @description: 判断是否为mobile
 * @param {type}
 * @return:
 */
export const isMobile = () => {
	const userAgentInfo = navigator.userAgent;
	const Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
	let flag = false;
	for (let v = 0; v < Agents.length; v++) {
		if (userAgentInfo.indexOf(Agents[v]) > 0) {
			flag = true;
			break;
		}
	}
	return flag;
};

/**
 * @description 扁平化数组对象(主要用来处理路由菜单)
 * @param {Array} menuList 所有菜单列表
 * @return array
 */
export const getFlatArr = (menuList: RouteRecord[]) => {
	const newMenuList: RouteRecord[] = JSON.parse(JSON.stringify(menuList));
	return newMenuList.reduce((pre, current) => {
		let flatArr = [...pre, current];
		if (current.children) flatArr = [...flatArr, ...getFlatArr(current.children)];
		return flatArr;
	}, []);
};
/**
 * @description 使用递归，过滤出需要渲染在左侧菜单的列表（剔除 isHide == true 的菜单）
 * @param {Array} menuList 所有菜单列表
 * @return array
 * */
export function getShowMenuList(menuList: RouteRecord[]) {
	const newMenuList: RouteRecord[] = JSON.parse(JSON.stringify(menuList));
	return newMenuList.filter(item => {
		item.children?.length && (item.children = getShowMenuList(item.children));
		return !item.hidden;
	});
}
